package com.xbongbong.saas.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.model.PaasFormDataEsModel;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.UnitEnum;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.paas.help.FormDataListSearchHelp;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.FormTitlePoJo;
import com.xbongbong.paas.pojo.SummaryDataPoJo;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.applist.pojo.AppListPojo;
import com.xbongbong.pro.applist.pojo.vo.ListAppVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.OnlinePaymentFlowDistributorEnum;
import com.xbongbong.saas.model.IndexTypeModel;
import com.xbongbong.saas.service.OnlinePaymentFlowService;
import com.xbongbong.saas.service.toolbox.help.AttrDecimalPrecisionHelper;
import com.xbongbong.saas.service.toolbox.help.SaasListDataPermissionHelp;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zcp
 * @version v1.0
 * @since v1.0
 */
@Service("onlinePaymentFlowService")
public class OnlinePaymentFlowServiceImpl implements OnlinePaymentFlowService {
    private static final Logger LOG = LoggerFactory.getLogger(OnlinePaymentFlowServiceImpl.class);
    @Resource
    private SaasListDataPermissionHelp saasListDataPermissionHelp;
    @Resource
    private FormDataListSearchHelp formDataListSearchHelp;
    @Resource
    private PaasFormDataEsModel paasFormDataEsModel;
    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;
    @Resource
    private AttrDecimalPrecisionHelper attrDecimalPrecisionHelper;
    @Resource
    private CommonHelp commonHelp;
    @Resource
    private IndexTypeModel indexTypeModel;

    @Override
    public ListAppVO formatOnlinePaymentFlowAppList(FormDataListDTO formDataListDTO, List<PaasFormDataEntityExt> esEntities, Integer totalCount) throws XbbException {
        List<AppListPojo> appListPojos = new ArrayList<>();
        Integer accuracy = BasicConstant.FOUR;
        for (PaasFormDataEntityExt entityExt : esEntities) {
            JSONObject data = entityExt.getData();
            Long paymentSheetId = entityExt.getId();
            Long appId = entityExt.getAppId();
            Long menuId = entityExt.getMenuId();
            Long formId = entityExt.getFormId();
            double amount = data.getDoubleValue(OnlinePaymentFlowDistributorEnum.AMOUNT.getAttr());
            double amountActual = data.getDoubleValue(OnlinePaymentFlowDistributorEnum.AMOUNT_ACTUAL.getAttr());

            Object amountObj =  AttrDecimalPrecisionHelper.parseThousandth(amount,accuracy);
            Object amountActualObj =  AttrDecimalPrecisionHelper.parseThousandth(amountActual, accuracy);
            data.put(OnlinePaymentFlowDistributorEnum.AMOUNT.getAttr(), amountObj);
            data.put(OnlinePaymentFlowDistributorEnum.AMOUNT_ACTUAL.getAttr(), amountActualObj);
            //转为百分号
            double feeRate = data.getDoubleValue(OnlinePaymentFlowDistributorEnum.FEE_RATE.getAttr());
            data.put(OnlinePaymentFlowDistributorEnum.FEE_RATE.getAttr(), commonHelp.getPercentFormat(feeRate, 3, 2));
            List<FormTitlePoJo> titleList = FastJsonHelper.getJsonArrOrDefaultFromFormData(data, BasicConstant.TITLE_LIST, new JSONArray()).toJavaList(FormTitlePoJo.class);
            List<SummaryDataPoJo> summaryList = FastJsonHelper.getJsonArrOrDefaultFromFormData(data, BasicConstant.SUMMARY_LIST, new JSONArray()).toJavaList(SummaryDataPoJo.class);
            List<SummaryDataPoJo> lablesList = FastJsonHelper.getJsonArrOrDefaultFromFormData(data, BasicConstant.LABELS_LIST, new JSONArray()).toJavaList(SummaryDataPoJo.class);
            JSONObject others = disposeOnlinePaymentFlowOthers(data);
            JSONObject superScript = new JSONObject();
            appListPojos.add(new AppListPojo(paymentSheetId, titleList, summaryList, superScript, lablesList, others, appId, menuId, formId));
        }
        ListAppVO listAppVO = new ListAppVO();
        listAppVO.setList(appListPojos);
        listAppVO.setTotalCount(totalCount);
        return listAppVO;
    }

    /**
     * 处理others字段
     *
     * @param data 在线流水数据
     * @return com.alibaba.fastjson.JSONObject
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private JSONObject disposeOnlinePaymentFlowOthers(JSONObject data) throws XbbException {
        JSONObject others = new JSONObject();
        //内部展示的是文字
        String statusStr = FastJsonHelper.getStringFromFormData(data, OnlinePaymentFlowDistributorEnum.STATUS.getAttr());
        double amount = data.getDoubleValue(OnlinePaymentFlowDistributorEnum.AMOUNT.getAttr());
        String amountStr = StringUtil.formatDouble(amount) + UnitEnum.MONEY.getName();
        others.put("amount", amountStr);
        others.put("status", statusStr);
        return others;
    }


}
